package com.palmwindActivity.textmodule;


import java.util.Vector;
import java.util.concurrent.ThreadPoolExecutor;

import com.palmwindActivity.R;
import com.palmwindActivity.app.AbstractActivity;
import com.palmwindActivity.app.AbstractServiceProxy;
import com.palmwindActivity.app.ICommConstants;
import com.palmwindActivity.app.IServiceProxyConstants;
import com.palmwindActivity.app.IServiceProxyListener;
import com.palmwindActivity.app.main.ThreadPoolManager;
import com.palmwindActivity.settings.SettingActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class TextReviewInput extends AbstractActivity implements TextWatcher, IServiceProxyListener {
	
	private static final int CMD_SYNC = 1000;
	private static final int CMD_CHANGE_IP = 1002;
	Button uploadButton;
	EditText editView;
	ListView listView;
	UpdateTextHandler updateTextHandler;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		this.setTitle(R.string.title_comment);
		this.setContentView(R.layout.textmoduleview);
		editView = (EditText)findViewById(R.id.EditText01);
		
		editView.addTextChangedListener(this);
		uploadButton = (Button)findViewById(R.id.Button01);
		uploadButton.setClickable(false);
		uploadButton.setEnabled(false);
		updateTextHandler = new UpdateTextHandler();
		uploadButton.setOnClickListener(updateTextHandler);
		listView = (ListView)findViewById(R.id.ListView01);
		CommentsListAdapter commentsListAdapter = new CommentsListAdapter(null, this);
		listView.setAdapter(commentsListAdapter);
		
	}
	
	protected void onStartDelegate()
	{
		
		ThreadPoolExecutor threadPoolExecutor = ThreadPoolManager.getInstance().getThreadPool(ThreadPoolManager.NET_WORK);
		TextUpdateProxy textUpdateProxy = new TextUpdateProxy(threadPoolExecutor, TextReviewInput.this);
		textUpdateProxy.requireUpdateComments("albert Ma", System.currentTimeMillis());
		TextReviewInput.this.setProgressBarIndeterminateVisibility(true);
	}

	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if(s != null && s.length() > 0)
		{
			uploadButton.setClickable(true);
			uploadButton.setEnabled(true);
		}
		else
		{
			uploadButton.setClickable(false);
			uploadButton.setEnabled(false);
		}
	}
	
	public boolean onCreateOptionsMenu (Menu menu)
	{
		MenuItem syncItem = menu.add(0, CMD_SYNC, 0, R.string.update_text);
		MenuItem changeIp = menu.add(0, CMD_CHANGE_IP, 0, R.string.title_input_ip);
		changeIp.setOnMenuItemClickListener(updateTextHandler);
		syncItem.setOnMenuItemClickListener(updateTextHandler);
		return super.onCreateOptionsMenu(menu);
	}
	class UpdateTextHandler implements View.OnClickListener, MenuItem.OnMenuItemClickListener
	{

		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v.getId() == R.id.Button01)
			{
				String commentText = editView.getText().toString();
				ThreadPoolExecutor threadPoolExecutor = ThreadPoolManager.getInstance().getThreadPool(ThreadPoolManager.NET_WORK);
				TextUploadProxy textUploadProxy = new TextUploadProxy(threadPoolExecutor, TextReviewInput.this);
				textUploadProxy.uploadUserComments("ABC", System.currentTimeMillis(), commentText);
				TextReviewInput.this.runOnUiThread(new Runnable()
				{

					public void run() {
						
						TextReviewInput.this.setProgressBarIndeterminateVisibility(true);
					}
					
				});
				
			}
		}

		public boolean onMenuItemClick(MenuItem item) {
			if(item.getItemId() == CMD_SYNC)
			{
				ThreadPoolExecutor threadPoolExecutor = ThreadPoolManager.getInstance().getThreadPool(ThreadPoolManager.NET_WORK);
				TextUpdateProxy textUpdateProxy = new TextUpdateProxy(threadPoolExecutor, TextReviewInput.this);
				textUpdateProxy.requireUpdateComments("albert Ma", System.currentTimeMillis());
				TextReviewInput.this.runOnUiThread(new Runnable()
				{

					public void run() {
						
						TextReviewInput.this.setProgressBarIndeterminateVisibility(true);
					}
					
				});
				return true;
			}
			else if (CMD_CHANGE_IP == item.getItemId())
			{
				Intent intent = new Intent();
				intent.setClass(TextReviewInput.this, SettingActivity.class);
				TextReviewInput.this.startActivity(intent);
			}
			return false;
		}
		
	}
	public void networkError(int errorCode, String actionId, final String errorMessage, AbstractServiceProxy proxy) {
		if(IServiceProxyConstants.ACTION_UPLOAD_TEXT.equalsIgnoreCase(actionId))
		{
			
		}
		else if(IServiceProxyConstants.ACTION_GET_TEXT.equalsIgnoreCase(actionId))
		{
			
		}
		this.runOnUiThread(new Runnable()
		{

			public void run() {
				TextReviewInput.this.setProgressBarIndeterminateVisibility(false);
				if(errorMessage != null && errorMessage.length() > 0)
				{
					Toast.makeText(TextReviewInput.this, errorMessage, 2000).show();
				}
				else
				{
					Toast.makeText(TextReviewInput.this, R.string.network_error, 2000).show();
				}
				
			}
			
		});
	}

	public void networkFinished(int status, String actionId,AbstractServiceProxy proxy) {
		if(IServiceProxyConstants.ACTION_UPLOAD_TEXT.equalsIgnoreCase(actionId))
		{
			this.runOnUiThread(new Runnable() {

				public void run() {
					TextReviewInput.this.setProgressBarIndeterminateVisibility(false);
					Toast.makeText(TextReviewInput.this,
							R.string.upload_success, 2000).show();
				}
			});
			try {
				Thread.sleep(1200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else if(IServiceProxyConstants.ACTION_GET_TEXT.equalsIgnoreCase(actionId))
		{
			if(proxy instanceof TextUpdateProxy)
			{
				TextUpdateProxy textUpdateProxy = (TextUpdateProxy)proxy;
				Vector<UserComment> comments = textUpdateProxy.getComments();
				final CommentsListAdapter commentsListAdapter = new CommentsListAdapter(comments, this);
				this.runOnUiThread(new Runnable() {

					public void run() {
						TextReviewInput.this.setProgressBarIndeterminateVisibility(false);
						listView.setAdapter(commentsListAdapter);
					}
				});
			}
		}
		
	}

}

class CommentsListAdapter extends BaseAdapter
{

	private Vector<UserComment> comments;
	private Activity activity;
	public CommentsListAdapter(Vector<UserComment> comments, Activity activity)
	{
		super();
		this.comments = comments;
		this.activity = activity;
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		if(this.comments != null)
		{
			return comments.size();
		}
		return 0;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(position >= 0 && comments != null && comments.size() > 0)
		{
			UserComment comment = comments.get(position);
			TextView textView = new TextView(activity);
			textView.setTextColor(0xFFFF00F0);
			AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
			textView.setLayoutParams(layoutParams);
			Time time = new Time();
			time.set(comment.getTimeStamp());
			textView.setText("User:" + comment.getUserName() + "  at Time:" + time.format("%Y%m%dT%H%M%S") +"\n" +comment.getComment());
			textView.setSingleLine(false);
			return textView;
		}
		return null;
	}
	
}

